package com.poem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.poem.constant.MessageConstant;
import com.poem.context.BaseContext;
import com.poem.dto.PoemsDTO;
import com.poem.dto.PoemsPageQueryDTO;
import com.poem.entity.Poems;
import com.poem.exception.BaseException;
import com.poem.exception.PoemsException;
import com.poem.mapper.FavoritesMapper;
import com.poem.mapper.PoemsMapper;
import com.poem.result.PageResult;
import com.poem.service.PoemsService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PoemsServiceImpl
 * @Author: YLY
 * @Description:
 * @Date: 2025/10/18
 * @Version: 1.0
 */
@Service
public class PoemsServiceImpl implements PoemsService {
    @Resource
    private PoemsMapper poemsMapper;
    @Resource
    private FavoritesMapper favoritesMapper;

    /**
     * 查询所有古诗
     * 
     * @return
     */
    public List<Poems> getAllPoems() {
        return poemsMapper.getAllPoems();
    }

    /**
     * 添加古诗
     * 
     * @param poemsDTO
     */
    public void addPoem(PoemsDTO poemsDTO) {
        Poems poem = new Poems();
        BeanUtils.copyProperties(poemsDTO, poem);
        poemsMapper.addPoem(poem);
    }

    /**
     * 修改古诗
     * 
     * @param poemsDTO
     */
    public void updatePoem(PoemsDTO poemsDTO) {
        // 检查诗文是否存在
        Poems existingPoem = poemsMapper.getById(poemsDTO.getId());
        if (existingPoem == null) {
            throw new BaseException(MessageConstant.POEM_NOT_EXISTS);
        }

        Poems poem = new Poems();
        BeanUtils.copyProperties(poemsDTO, poem);
        poemsMapper.updatePoem(poem);
    }

    /**
     * 批量删除古诗
     * 
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {

        // 将ids赋值给poemIds
        List<Long> poemIds = new ArrayList<>(ids);

        // 在删除古诗的时候，需要检查是否有收藏该古诗的用户，如果有，则不能删除，需返回该古诗已被收藏无法删除。
        String resourceType = "poem";
        for (Long resourceId : poemIds) {
            boolean checked = favoritesMapper.checkFavorite(resourceType, resourceId, null);
            // 根据id查询哪首古诗被收藏
            Poems poem = poemsMapper.getById(resourceId);
            String title = poem.getTitle();
            if (checked) {
                poemIds.remove(resourceId);
                throw new PoemsException(title + MessageConstant.POEM_ALREADY_FAVORITES);
                // 从ids中删除该古诗的id
            }

        }
        poemsMapper.deleteBatch(ids);
    }

    public PageResult pageQuery(PoemsPageQueryDTO poemsPageQueryDTO) {

        PageHelper.startPage(poemsPageQueryDTO.getPage(), poemsPageQueryDTO.getPageSize());

        Page<Poems> page = poemsMapper.pageQuery(poemsPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询古诗
     * 
     * @param id
     * @return
     */
    public Poems getById(Long id) {

        return poemsMapper.getById(id);
    }

    /**
     * 根据typeId查询古诗数量
     * 
     * @param ids
     * @return
     */
    public Map<Long, Integer> countByType(List<Long> ids) {
        Map<Long, Integer> countMap = new HashMap<>();
        for (Long typeId : ids) {
            Integer count = poemsMapper.countByType(typeId);
            countMap.put(typeId, count);
        }
        return countMap;
    }

}
